package com.xb.p6_menu_basic;

import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        textView=findViewById(R.id.tv_show);

        //noinspection SimplifiableIfStatement
        /*if (id == R.id.action_settings) {
            textView.setText("选中了“设置");
            return true;
        }
        if (id == R.id.action_help) {
            textView.setText("选中了“帮助");
            return true;
        }
        if (id == R.id.action_file) {
            textView.setText("选中了“文件");
            return true;
        }*/

        switch (id){
            case R.id.action_settings:
                textView.setText("选中了“设置");
                return true;
            case R.id.action_help:
                textView.setText("选中了“帮助");
                return true;
            case R.id.action_file:
                textView.setText("选中了“文件");
                return true;
            case R.id.action_file_open:
                textView.setText("选中了“打开文件");
                return true;
            case R.id.action_file_close:
                textView.setText("选中了“关闭文件");
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

    //执行的是这个方法，上面的setting的事件将不再执行
    public void setting(MenuItem menuItem){
        textView=findViewById(R.id.tv_show);
        textView.setText("执行了setting方法中：选中了“设置");
    }
}
